package org.mapdroid.search;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;

import org.mapdroid.R;
import org.mapdroid.data.adt.MdService;
import org.mapdroid.maps.gmaps.overlay.MdMarkerItemizedOverlay;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ServiceNameView extends Activity implements OnKeyListener, OnClickListener, OnItemClickListener{
	private EditText searchName;
	//private List <String> name;
	private ListView results;
	private Button searchButton, cancelButton;
	private List<MdService> serviceList;
	private List<MdService> responseList;
	
	public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.service_name_view);
        
        searchName = (EditText) findViewById(R.id.search_name);	
        results = (ListView) findViewById(R.id.search_name_result);
        searchButton = (Button) findViewById(R.id.search_b);
        cancelButton = (Button) findViewById(R.id.cancel_b);
        results.setOnItemClickListener(this);
        searchName.setOnKeyListener(this);
        searchButton.setOnClickListener(this);
        cancelButton.setOnClickListener(this);
        
        serviceList = super.getIntent().getParcelableArrayListExtra(MdMarkerItemizedOverlay.LIST);
        responseList = new LinkedList<MdService>();
        
        if(serviceList==null){
        	Toast.makeText(this, R.string.no_services_notify, Toast.LENGTH_SHORT).show();
        	finish();
        }
    }
    
    @Override
    public void onDestroy() {
    	super.onDestroy();
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    }

    @Override
    public void onResume(){
    	super.onResume();
    }
    
    public List<MdService> search(String nome){
    	List<MdService> returnList = new LinkedList<MdService>();
    	responseList = new LinkedList<MdService>();
    	try{
    		ListIterator<MdService> i = serviceList.listIterator();
    		while(i.hasNext()) {
    			MdService tmpService = i.next();
    			String tmpString = tmpService.getName().toUpperCase(Locale.getDefault());
    			nome = nome.toUpperCase(Locale.getDefault());
    			
    			if(tmpString.contains(nome)){
    				returnList.add(tmpService);
    				responseList.add(tmpService);
    			}
    		}
    	
    		if(returnList.size()==0)
    			return null;
    		
    		return returnList;
    		
		} catch(IllegalArgumentException e){
    		return null;
    	} catch(ClassCastException f){
			return null;
		}
    }

    public List<String> createNameList(List<MdService> lista){
    	try{
    		List<String> strings = new LinkedList<String>();
    		ListIterator<MdService> i = lista.listIterator();
	    	while(i.hasNext()) {
				MdService tmp =(MdService)i.next();
				strings.add(tmp.getName());
			}
	    	if(strings.size()==0)
	    		return null;

	    	return strings;
    	}catch(ClassCastException E){
    		return null;
    	}
    }

        
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event){
    	Editable name = searchName.getText();
		switch(keyCode){
			case KeyEvent.KEYCODE_DPAD_CENTER:
				
			case KeyEvent.KEYCODE_ENTER:
				List<MdService> responseList = search(name.toString());
				if(responseList!=null){
	    			List<String> strings = createNameList(responseList);
					results.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
					return true;
				}
				else{
					Toast.makeText(this, R.string.no_service_results_notify, Toast.LENGTH_LONG).show();
				}
				break;
		}
    	return false;
    }
    
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		int action = arg0.getId();
		switch(action){
			case R.id.search_b:
				Editable name = searchName.getText();
				List<MdService> responseList = search(name.toString());
				if(responseList!=null){
	    			List<String> strings = createNameList(responseList);
					results.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
				}
				else{
					Toast.makeText(this, R.string.no_service_results_notify, Toast.LENGTH_LONG).show();
				}
				break;
			case R.id.cancel_b:
				setResult(RESULT_CANCELED);
				finish();
				break;
		}
		
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
		// TODO Auto-generated method stub
		try{
			MdService clickedService = responseList.get(index);
			boolean isParking = clickedService.getCategory()==0 ? true : false;
			
			Intent resultIntent = new Intent();
			resultIntent.putExtra(MdMarkerItemizedOverlay.OBJECT, clickedService);
			resultIntent.putExtra(MdMarkerItemizedOverlay.OVERLAY_DATA, isParking);
			setResult(RESULT_OK, resultIntent);
			finish();
		}
		catch(IndexOutOfBoundsException e){
			e.printStackTrace();
		}
	}
}
